﻿Public Class FrmMain

	Private TimeEditMain As New TimeEdit()
	Private _LocalTime As TimeSpan

	Private Sub StartTimer(ByVal sender As Object)
		_LocalTime = TimeEditMain.EnteredTime
		UpdateTimerLabel()
		TmrMain.Start()
	End Sub

	Private Sub FrmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
		Me.Controls.Add(TimeEditMain)
		AddHandler TimeEditMain.TimeChanged, AddressOf StartTimer
	End Sub


	Private Sub ShowTimeEdit()
		TimeEditMain.Show()
		TimeEditMain.BringToFront()
		TimeEditMain.Select()
	End Sub

	Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles LblTimer.Click
		ShowTimeEdit()
	End Sub

	Private Sub StopTimer()
		TmrMain.Stop()
	End Sub

	Private Sub UpdateTimerLabel()
		LblTimer.Text = _LocalTime.ToString("c")
	End Sub

	Private Sub TmrMain_Tick(sender As Object, e As System.EventArgs) Handles TmrMain.Tick
		_LocalTime = _LocalTime.Subtract(TimeSpan.FromMilliseconds(TmrMain.Interval))
		If _LocalTime.TotalSeconds < 0 Then
			My.Computer.Audio.Play(My.Resources.alarm, AudioPlayMode.Background)
			NtfMain.ShowBalloonTip(5000, TxtMeetingLabel.Text, "Time is up!", ToolTipIcon.Info)
			StopTimer()
			Exit Sub
		End If

		UpdateTimerLabel()
	End Sub

	Private Sub TxtMeetingLabel_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TxtMeetingLabel.KeyDown
		If e.KeyCode = Keys.Enter Then
			TblTimer.Select()
			e.SuppressKeyPress = True
			e.Handled = True
		End If
	End Sub


	Private Sub NtfMain_MouseDoubleClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles NtfMain.MouseDoubleClick
		Me.Visible = Not Me.Visible
	End Sub

	Private Sub MnuItmExit_Click(sender As System.Object, e As System.EventArgs) Handles MnuItmExit.Click
		Me.Close()
	End Sub


#Region "Dragging code"
	' Stolen : http://www.vbforums.com/showthread.php?t=537720

	'Declare the variables
	Private _drag As Boolean
	Private _mousex As Integer
	Private _mousey As Integer

	Private Sub PnlDrag_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PnlDrag.MouseDown
		_drag = True	'Sets the variable drag to true.
		_mousex = Windows.Forms.Cursor.Position.X - Me.Left	'Sets variable mousex
		_mousey = Windows.Forms.Cursor.Position.Y - Me.Top 'Sets variable mousey
	End Sub

	Private Sub PnlDrag_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PnlDrag.MouseMove
		'If drag is set to true then move the form accordingly.
		If _drag Then
			Me.Top = Windows.Forms.Cursor.Position.Y - _mousey
			Me.Left = Windows.Forms.Cursor.Position.X - _mousex
		End If
	End Sub

	Private Sub PnlDrag_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PnlDrag.MouseUp
		_drag = False 'Sets drag to false, so the form does not move according to the code in MouseMove
	End Sub
#End Region

End Class
